package com.cxd.app.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringEscapeUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.cxd.app.constants.PublicConstants;
import com.cxd.app.entity.AppQuery;
import com.cxd.app.entity.base.AppVersionBase;
import com.cxd.app.entity.base.AuthInfoBase;
import com.cxd.app.entity.base.UserBase;
import com.cxd.app.service.OtherService;
import com.cxd.base.common.exception.CxdException;
import com.cxd.base.controller.BaseController;
import com.cxd.base.util.ArithUtil;
import com.cxd.base.util.BeanTools;
import com.cxd.base.util.HashCrypt;
import com.cxd.base.util.HtmlElementUtil;
import com.cxd.base.util.MapUtil;
import com.cxd.base.util.NumberUtil;
import com.cxd.base.util.ParamUtil;
import com.cxd.base.util.WithdrawalUtil;
import com.cxd.base.util.date.DateStyle;
import com.cxd.base.util.date.DateUtil;

/**
 * <一句话功能简述> <功能详细描述>
 * @author wtj
 * @date 2015年12月5日 上午11:24:09
 * @company CXD
 * @version [Copyright (c) 2015 V001]
 */
@Controller("OtherController")
@RequestMapping("otherc")
public class OtherController extends BaseController{
	
	public final String NO_SHOW_MSG = "用户名或密码错误或没有权限查看";
	private static final Logger logger = Logger.getLogger(OtherController.class);
	
	@Resource
	public BeanTools bt;
	
	@Resource 
	public OtherService otherService;
	/**
	 * 检测版本
	 * @author wtj
	 * @param request
	 * @param response
	 * @param value
	 * @throws IOException
	 */
	@RequestMapping(value = "version", method = {RequestMethod.GET,RequestMethod.POST })
	public void version(HttpServletRequest request,HttpServletResponse response,String value)throws IOException {
		JSONObject json = new JSONObject();
		try {
			Map<String,String> param = MapUtil.jsonToMapString(value);
			String packageName=param.get("packageName");// 包名
			if(StringUtils.isBlank(packageName) ){
				this.jsonMsg(json, 500, "参数异常");
				json.put("result",new String[]{});
			}else{
				AppVersionBase avBean = bt.queryBean("APP_VERSION", "ID", packageName.trim(), AppVersionBase.class);
				if(avBean == null){
					this.jsonMsg(json, 500, "获取数据异常");
					json.put("result",new String[]{});
				}else{
					json.put("result",avBean);
					if(json.get("status")==null){
						this.jsonMsg(json, 200, "成功");
					}
				}
			}
		} catch(CxdException e){
			super.cxdJsonException(json, e,value);
		}catch (Exception e) {
			super.jsonException(json, e, logger);
		}
		super.responseSendMsg(response, json);
	}
	
	/**
	 * 总用户投资收益信息
	 * @author wtj
	 * @date 2015年12月8日下午4:45:40
	 * @param request
	 * @param response
	 * @param value
	 * @throws IOException
	 */
	@RequestMapping(value = "moneyStatic", method = {RequestMethod.GET,RequestMethod.POST })
	public void moneyStatic(HttpServletRequest request,HttpServletResponse response)throws IOException {
		JSONObject json = new JSONObject();
		try {
			otherService.moneyStatic(json);
			if(json.get("status")==null){
				this.jsonMsg(json, 200, "成功");
			}
		} catch(CxdException e){
			super.cxdJsonException(json, e,"无参数");
		}catch (Exception e) {
			super.jsonException(json, e, logger);
		}
		super.responseSendMsg(response, json);
	}
	
	/**
	 * 获取APP banner
	 * @author wtj
	 * @date 2015年12月9日下午8:05:33
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping(value = "loadBanners", method = {RequestMethod.GET,RequestMethod.POST })
	public void loadBanners(HttpServletRequest request,HttpServletResponse response)throws IOException {
		JSONObject json = new JSONObject();
		try {
			json = otherService.loadBanners(json);
			if(json.get("status")==null){
				this.jsonMsg(json, 200, "成功");
			}
		} catch(CxdException e){
			super.cxdJsonException(json, e,"无参数");
		}catch (Exception e) {
			super.jsonException(json, e, logger);
		}
		super.responseSendMsg(response, json);
	}
	
	/**
	 * 意见反馈
	 * @author wtj
	 * @date 2015年12月9日下午8:24:32
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping(value = "feedback", method = {RequestMethod.GET,RequestMethod.POST })
	public void feedback(HttpServletRequest request,HttpServletResponse response,String value)throws IOException {
		JSONObject json = new JSONObject();
		try {
			json = otherService.feedback(json,value);
			if(json.get("status")==null){
				this.jsonMsg(json, 200, "成功");
			}
		} catch(CxdException e){
			super.cxdJsonException(json, e,value);
		}catch (Exception e) {
			super.jsonException(json, e, logger);
		}
		super.responseSendMsg(response, json);
	}
	
	/**
	 * 获取公告
	 * @author wtj
	 * @date 2015年12月15日下午4:15:12
	 * @param request
	 * @param response
	 * @param value
	 * @throws IOException
	 */
	@RequestMapping(value = "loadNews", method = {RequestMethod.GET,RequestMethod.POST })
	public void loadNews(HttpServletRequest request,HttpServletResponse response,String value)throws IOException {
		JSONObject json = new JSONObject();
		try {
			json = otherService.loadNews(json,value);
			if(json.get("status")==null){
				this.jsonMsg(json, 200, "成功");
			}
		} catch(CxdException e){
			super.cxdJsonException(json, e,value);
		}catch (Exception e) {
			super.jsonException(json, e, logger);
		}
		super.responseSendMsg(response, JSON.toJSONStringWithDateFormat(json, "yyyy-MM-dd", SerializerFeature.WriteDateUseDateFormat));
	}
	
	/**新的获取最新公告
	 * @author wtj
	 * @date 2016年3月9日下午3:51:26
	 * @param request
	 * @param response
	 * @param value
	 * @throws IOException
	 */
	@RequestMapping(value = "loadNoticeList", method = {RequestMethod.GET,RequestMethod.POST })
	public void loadNewNoticeList(HttpServletRequest request,HttpServletResponse response,String value)throws IOException {
		JSONObject json = new JSONObject();
		try {
			json = otherService.loadNewNoticeList(json,value);
			if(json.get("status")==null){
				this.jsonMsg(json, 200, "成功");
			}
		} catch(CxdException e){
			super.cxdJsonException(json, e,value);
		}catch (Exception e) {
			super.jsonException(json, e, logger);
		}
		super.responseSendMsg(response, JSON.toJSONStringWithDateFormat(json, "yyyy-MM-dd", SerializerFeature.WriteDateUseDateFormat));
	} 
	
	/**
	 * 获取最新公告详情
	 * @author wtj
	 * @date 2016年3月14日下午3:17:19
	 * @param request
	 * @param response
	 * @param value
	 * @throws IOException
	 */
	@RequestMapping(value = "loadNoticeDetail", method = {RequestMethod.GET,RequestMethod.POST })
	public void loadNewNoticeDetail(HttpServletRequest request,HttpServletResponse response,String value)throws IOException {
		JSONObject json = new JSONObject();
		try {
			json = otherService.loadNoticeDetail(json,value);
			if(json.get("status")==null){
				this.jsonMsg(json, 200, "成功");
			}
		} catch(CxdException e){
			super.cxdJsonException(json, e,value);
		}catch (Exception e) {
			super.jsonException(json, e, logger);
		}
		super.responseSendMsg(response, JSON.toJSONStringWithDateFormat(json, "yyyy-MM-dd", SerializerFeature.WriteDateUseDateFormat));
	} 
	
	/**
	 * 获取还款通知
	 * @author wtj
	 * @date 2016年3月9日下午3:52:12
	 * @param json
	 * @param value
	 * @return
	 */
	@RequestMapping(value = "loadRepayList", method = {RequestMethod.GET,RequestMethod.POST })
	public void loadRepayList(HttpServletRequest request,HttpServletResponse response,String value)throws IOException {
		JSONObject json = new JSONObject();
		try {
			json = otherService.loadRepayList(json,value);
			if(json.get("status")==null){
				this.jsonMsg(json, 200, "成功");
			}
		} catch(CxdException e){
			super.cxdJsonException(json, e,value);
		}catch (Exception e) {
			super.jsonException(json, e, logger);
		}
		super.responseSendMsg(response, JSON.toJSONStringWithDateFormat(json, "yyyy-MM-dd", SerializerFeature.WriteDateUseDateFormat));
	} 
	
	/**
	 * 检查是否有新公告
	 * @author wtj
	 * @date 2016年3月10日上午11:44:30
	 * @param request
	 * @param response
	 * @param value
	 * @throws IOException
	 */
//	@RequestMapping(value = "checkNewNotice", method = {RequestMethod.GET,RequestMethod.POST })
//	public void checkNewNotice(HttpServletRequest request,HttpServletResponse response,String value)throws IOException {
//		JSONObject json = new JSONObject();
//		try {
//			json = otherService.checkNewNotice(json,value);
//			if(json.get("status")==null){
//				this.jsonMsg(json, 200, "成功");
//			}
//		} catch(CxdException e){
//			super.cxdJsonException(json, e,value);
//		}catch (Exception e) {
//			super.jsonException(json, e, logger);
//		}
//		super.responseSendMsg(response, JSON.toJSONStringWithDateFormat(json, "yyyy-MM-dd", SerializerFeature.WriteDateUseDateFormat));
//	} 
	/**
	 * 保存用户手机标识
	 * @author wtj
	 * @date 2016年3月10日下午12:00:41
	 * @param request
	 * @param response
	 * @param value
	 * @throws IOException
	 */
	@RequestMapping(value = "uploadDeviceToken", method = {RequestMethod.GET,RequestMethod.POST })
	public void uploadDeviceToken(HttpServletRequest request,HttpServletResponse response,String value)throws IOException {
		JSONObject json = new JSONObject();
		try {
			json = otherService.uploadDeviceToken(json,value);
			if(json.get("status")==null){
				this.jsonMsg(json, 200, "成功");
			}
		} catch(CxdException e){
			super.cxdJsonException(json, e,value);
		}catch (Exception e) {
			super.jsonException(json, e, logger);
		}
		super.responseSendMsg(response, JSON.toJSONStringWithDateFormat(json, "yyyy-MM-dd", SerializerFeature.WriteDateUseDateFormat));
	}
	
	
	/**
	 * 字典接口
	 * @author wtj
	 * @date 2015年12月18日下午6:09:06
	 * @param request
	 * @param response
	 * @param value
	 * @throws IOException 
	 */
	@RequestMapping(value = "cxdAppDict", method = {RequestMethod.GET,RequestMethod.POST })
	public void cxdAppDict(HttpServletRequest request,HttpServletResponse response,String value) throws IOException{
		JSONObject json = new JSONObject();
		Map<String,Object> map = new HashMap<String,Object>();
//		
//		// 实名认证常量参数
//		Map<String,String> realnameAuthMap = new HashMap<String,String>();
//		realnameAuthMap.put("callbackUrl", AppConstants.ResponseWebUrl.PRE_RESPONSE_URL+ YeePayConstants.OperationType.CREATE_ACCOUNT);
//		realnameAuthMap.put("notifyUrl", AppConstants.ResponseS2SUrl.PRE_RESPONSE_URL+ YeePayConstants.OperationType.CREATE_ACCOUNT);
//		realnameAuthMap.put("ypActionUrl",AppConstants.RequestUrl.APP_AUTHENTICATION_URL);
//		realnameAuthMap.put("requestNoPrefix", YeePayConstants.RequestNoPre.CREATE_ACCOUNT);
//		realnameAuthMap.put("info","实名认证回调url");
//		map.put("realnameAuth", realnameAuthMap);
//		
//		// 充值常亮
//		Map<String,String> rechargeMap = new HashMap<String,String>();
//		rechargeMap.put("callbackUrl", AppConstants.ResponseWebUrl.PRE_RESPONSE_URL+ YeePayConstants.OperationType.RECHARGE);
//		rechargeMap.put("notifyUrl", AppConstants.ResponseS2SUrl.PRE_RESPONSE_URL+ YeePayConstants.OperationType.RECHARGE);
//		
//		rechargeMap.put("ypActionUrl",AppConstants.RequestUrl.APP_RECHARGE_URL);
//		rechargeMap.put("requestNoPrefix", YeePayConstants.RequestNoPre.RECHARGE);
//		rechargeMap.put("info","充值回调url");
//		map.put("recharge", rechargeMap);
//		
//		// 提现常量
//		Map<String,String> extractionMap = new HashMap<String,String>();
//		extractionMap.put("callbackUrl", AppConstants.ResponseWebUrl.PRE_RESPONSE_URL+ YeePayConstants.OperationType.WITHDRAW_CASH);
//		extractionMap.put("notifyUrl", AppConstants.ResponseS2SUrl.PRE_RESPONSE_URL+ YeePayConstants.OperationType.WITHDRAW_CASH);
//		extractionMap.put("ypActionUrl", AppConstants.RequestUrl.APP_WITHDRAW_URL);
//		extractionMap.put("requestNoPrefix", YeePayConstants.RequestNoPre.WITHDRAW_CASH);
//		extractionMap.put("info","提现回调url");
//		map.put("extraction", extractionMap);
//		
//		// 绑卡常量
//		Map<String,String> bindCardMap = new HashMap<String,String>();
//		bindCardMap.put("callbackUrl", YeePayConstants.ResponseWebUrl.PRE_RESPONSE_URL+ YeePayConstants.OperationType.BINDING_YEEPAY_BANKCARD);
//		bindCardMap.put("notifyUrl", YeePayConstants.ResponseS2SUrl.PRE_RESPONSE_URL+ YeePayConstants.OperationType.BINDING_YEEPAY_BANKCARD);
//		bindCardMap.put("ypActionUrl", AppConstants.RequestUrl.APP_BINDCARD_URL);
//		bindCardMap.put("requestNoPrefix", YeePayConstants.RequestNoPre.BINDING_YEEPAY_BANKCARD);
//		bindCardMap.put("info","绑卡回调url");
//		map.put("bindCard", bindCardMap);
//		
//		// 项目投资
//		Map<String,String> investMap = new HashMap<String,String>();
//		investMap.put("callbackUrl", AppConstants.ResponseWebUrl.PRE_RESPONSE_URL+ YeePayConstants.OperationType.INVEST);
//		investMap.put("notifyUrl", AppConstants.ResponseS2SUrl.PRE_RESPONSE_URL+ YeePayConstants.OperationType.INVEST);
//		investMap.put("ypActionUrl", AppConstants.RequestUrl.APP_INVEST_URL);
//		investMap.put("requestNoPrefix", YeePayConstants.RequestNoPre.INVEST);
//		investMap.put("info","项目投资回调url");
//		map.put("invest", investMap);
		
		/**
		 * 借款查询条件
		 */
		List<AppQuery> loansRateList = new ArrayList<AppQuery>();
			loansRateList.add(new AppQuery("全部收益","",""));
			loansRateList.add(new AppQuery("10%以下","","0.1"));
			loansRateList.add(new AppQuery("10%-15%","0.1","0.15"));
			loansRateList.add(new AppQuery("15%-20%","0.15","0.2"));
			loansRateList.add(new AppQuery("20以上","0.2",""));
			map.put("loansRateQuery", loansRateList);
		
		List<AppQuery> loansDateList = new ArrayList<AppQuery>();
			loansDateList.add(new AppQuery("全部期限","",""));
			loansDateList.add(new AppQuery("3个月以下","","3"));
			loansDateList.add(new AppQuery("3-6个月","3","6"));
			loansDateList.add(new AppQuery("6-10个月","6","10"));
			loansDateList.add(new AppQuery("12个月以上","12",""));
			map.put("loansDateQuery", loansDateList);
		
		/**
		 * 账单查询
		 */
		List<AppQuery> billStatusList = new ArrayList<AppQuery>();
			billStatusList.add(new AppQuery("全部",""));
			billStatusList.add(new AppQuery("充值","recharge_success"));
			billStatusList.add(new AppQuery("投资","invest_success"));
			billStatusList.add(new AppQuery("提现","withdraw_success"));
			billStatusList.add(new AppQuery("回款","normal_repay"));
			map.put("billStatusQuery", billStatusList);
		
		List<AppQuery> billDateList = new ArrayList<AppQuery>();
			billDateList.add(new AppQuery("全部","",""));
			billDateList.add(new AppQuery("1个月以内","","1"));
			billDateList.add(new AppQuery("1-3个月","1","3"));
			billDateList.add(new AppQuery("3-6个月","3","6"));
			billDateList.add(new AppQuery("6-12个月","6","12"));
			billDateList.add(new AppQuery("12个月以上","12",""));
			map.put("billDateQuery", billDateList);
		
		/**
		 * 项目投资记录
		 */
		List<AppQuery> loanInvestStatusList = new ArrayList<AppQuery>();
			loanInvestStatusList.add(new AppQuery("全部",""));
			loanInvestStatusList.add(new AppQuery("投资中","bid_success"));
			loanInvestStatusList.add(new AppQuery("还款中","repaying"));
			loanInvestStatusList.add(new AppQuery("已流标","cancel"));
			loanInvestStatusList.add(new AppQuery("已完成","complete"));
			loanInvestStatusList.add(new AppQuery("已转出","transfered"));
			map.put("loanInvestStatus", loanInvestStatusList);
			
		
		/**
		 * 奖品时间
		 */
		List<AppQuery> prizeDateList = new ArrayList<AppQuery>();
		prizeDateList.add(new AppQuery("全部","",""));
		prizeDateList.add(new AppQuery("3个月以下","0","3"));
		prizeDateList.add(new AppQuery("3-6个月以下","3","6"));
		prizeDateList.add(new AppQuery("6-10个月以下","6","10"));
		map.put("prizeDateList", prizeDateList);
		
		/**
		 * 发货状态
		 */
		List<AppQuery> prizeSendStatusList = new ArrayList<AppQuery>();
		prizeSendStatusList.add(new AppQuery("全部",""));
		prizeSendStatusList.add(new AppQuery("收货信息确认中","verify"));
		prizeSendStatusList.add(new AppQuery("待发货","delivery_to"));
		prizeSendStatusList.add(new AppQuery("已发货","delivery_complete"));
//		prizeSendStatusList.add(new AppQuery("已送达","delivery_success"));
//		prizeSendStatusList.add(new AppQuery("未收货","delivery_fail"));
		map.put("prizeSendStatusList", prizeSendStatusList);
			
		/**
		 * 业务类型
		 */
		List<AppQuery> pointModeList = new ArrayList<AppQuery>();
		pointModeList.add(new AppQuery("全部",""));
		pointModeList.add(new AppQuery("积分抽奖","prize"));
		pointModeList.add(new AppQuery("积分兑换","cost"));
		pointModeList.add(new AppQuery("项目投资","invest"));
		map.put("pointModeList", pointModeList);
		
		json.put("result", map);
		json.put("status", 200);
		json.put("msg", "获取成功");
		super.responseSendMsg(response, json);
	}
	
	@RequestMapping(value = "withdrawalFee", method = {RequestMethod.GET,RequestMethod.POST })
	public void withdrawalFee(HttpServletRequest request,HttpServletResponse response,String value) throws IOException{
		JSONObject json = new JSONObject();
		try {
			Map<String, String> param = MapUtil.jsonToMapString(value);
			// 提现类型 0:T+1 1:立即
			String type = param.get("type");
			// 提现金额
			Double money = Double.parseDouble(param.get("money").toString());
			ParamUtil.notNullParam("type:"+type,"money:"+money);
			json.put("result", WithdrawalUtil.getWithdrawalFee(type,money));
			if(json.get("status")==null){
				this.jsonMsg(json, 200, "获取手续费成功");
			}
		} catch(CxdException e){
			super.cxdJsonException(json, e,value);
		}catch (Exception e) {
			super.jsonException(json, e, logger);
		}
		super.responseSendMsg(response, JSON.toJSONStringWithDateFormat(json, "yyyy-MM-dd", SerializerFeature.WriteDateUseDateFormat));
	} 
	
	/**
	 * 获取用户最后验证码
	 * <一句话功能简述> <功能详细描述>
	 * @author wtj
	 * @date 2016年1月29日上午10:54:09
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping(value = "getcode", method = {RequestMethod.GET,RequestMethod.POST })
	public void getcode(HttpServletRequest request,HttpServletResponse response,String value)throws IOException {
		super.requestLog("getcode",value);
		JSONObject json = new JSONObject();
		try {
			@SuppressWarnings("unchecked")
			Map<String, String> param = JSON.parseObject(value, HashMap.class);
			String loginId = param.get("loginId");
			String loginPassword = param.get("loginPassword");
			String phone = param.get("phone");
			ParamUtil.notNullParam("loginId:"+loginId,"loginPassword:"+loginPassword,"phone:"+phone);
			if(!isShow(loginId,loginPassword)){
				json.put("msg",NO_SHOW_MSG);
				json.put("status","500");
			}else{
				AuthInfoBase authInfo = bt.queryBean("select * from auth_info a where a.auth_target = '"+phone+"' ORDER BY generation_time desc LIMIT 0,1",AuthInfoBase.class);
				if(authInfo==null){
					json.put("status", "500");
					json.put("msg", "没有查询到验证码,请用户重新发送");
				}else{
					json.put("验证码", authInfo.getAuthCode());
					json.put("发送时间", DateUtil.dateToString(authInfo.getGenerationTime(),DateStyle.YYYY_MM_DD_HH_MM_SS.getValue()));
				}
			}
		} catch(CxdException e){
			super.cxdJsonException(json, e,"无参数");
		}catch (Exception e) {
			super.jsonException(json, e, logger);
		}
		super.responseSendMsg(response, json);
	} 
	
	/**
	 * 获取抽检人名单
	 * @author wtj
	 * @date 2016年2月1日下午2:04:12
	 * @param request
	 * @param response
	 * @param value
	 * @throws IOException
	 */
	@RequestMapping(value = "load_draw_list", method = {RequestMethod.GET,RequestMethod.POST })
	public void load_draw_list(HttpServletRequest request,HttpServletResponse response,String value)throws IOException {
		super.requestLog("load_draw_list",value);
		StringBuffer sb = new StringBuffer();
		try {
			@SuppressWarnings("unchecked")
			Map<String, String> param = JSON.parseObject(value, HashMap.class);
			String loginId = param.get("loginId");
			String loginPassword = param.get("loginPassword");
			ParamUtil.notNullParam("loginId:"+loginId,"loginPassword:"+loginPassword);
			
			if(!isShow(loginId,loginPassword)){
				sb.append(NO_SHOW_MSG);
			}else{
				List<Map<String,Object>> list = bt.queryListMap("select DISTINCT user_id from luck_draw_details");
				sb.append("<table width='100%'>");
				sb.append("<tr>"+setTd("用户名")+"</td>");
				for(Map<String,Object> map :list){
					sb.append("<tr>"+setTd(map.get("user_id").toString())+"</td>");
				}
				sb.append("</table>");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		super.responseSendMsg(response, sb.toString(),"text/html");
	}
	/**
	 * 用户获得抽奖记录
	 * @author wtj
	 * @date 2016年2月1日下午2:42:49
	 * @param request
	 * @param response
	 * @param value
	 * @throws IOException
	 */
	@RequestMapping(value = "user_draw_list", method = {RequestMethod.GET,RequestMethod.POST })
	public void user_draw_list(HttpServletRequest request,HttpServletResponse response,String value)throws IOException {
		super.requestLog("user_draw_list",value);
		StringBuffer sb = new StringBuffer();
		try {
			@SuppressWarnings("unchecked")
			Map<String, String> param = JSON.parseObject(value, HashMap.class);
			String loginId = param.get("loginId");
			String loginPassword = param.get("loginPassword");
			String userId = param.get("userId");
			ParamUtil.notNullParam("loginId:"+loginId,"loginPassword:"+loginPassword);
			
			if(!isShow(loginId,loginPassword)){
				sb.append(NO_SHOW_MSG);
			}else{
				String where="";
				if(StringUtils.isNotBlank(userId)){
					where = " and ld.user_id = '"+userId+"'";
				}
				String sql = "select ld.user_id,ld.ctime,ld.consume_point,ld.prize_name,us.mobile_number from luck_draw_details ld,`user` us where us.id = ld.user_id " +where+ " ORDER BY ld.ctime desc";
				List<Map<String,Object>> list = bt.queryListMap(sql);
				sb.append("<table width='100%'>");
				sb.append("<tr>"+setTd("用户名","抽奖时间","消耗积分","获得奖品","用户手机号")+"</td>");
				for(Map<String,Object> map :list){
					sb.append("<tr>"+setTd(map.get("user_id"),map.get("ctime"),map.get("consume_point"),map.get("prize_name"),map.get("mobile_number"))+"</td>");
				}
				sb.append("</table>");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		super.responseSendMsg(response, sb.toString(),"text/html");
	}
	
	/**
	 * 用户收获地址
	 * @author wtj
	 * @date 2016年2月1日下午2:43:46
	 * @param request
	 * @param response
	 * @param value
	 * @throws IOException
	 */
	@RequestMapping(value = "receive_address", method = {RequestMethod.GET,RequestMethod.POST })
	public void receive_address(HttpServletRequest request,HttpServletResponse response,String value)throws IOException {
		super.requestLog("user_draw_list",value);
		StringBuffer sb = new StringBuffer();
		try {
			@SuppressWarnings("unchecked")
			Map<String, String> param = JSON.parseObject(value, HashMap.class);
			String loginId = param.get("loginId");
			String loginPassword = param.get("loginPassword");
			String userId = param.get("userId");
			ParamUtil.notNullParam("loginId:"+loginId,"loginPassword:"+loginPassword);
			
			if(!isShow(loginId,loginPassword)){
				sb.append(NO_SHOW_MSG);
			}else{
				String where="";
				if(StringUtils.isNotBlank(userId)){
					where = " where user_id = '"+userId+"'";
				}
				String sql = "select user_id,contacts_address,ctime from receive_address "+where+" ORDER BY ctime desc";
				List<Map<String,Object>> list = bt.queryListMap(sql);
				sb.append("<table width='100%'>");
				sb.append("<tr>"+setTd("用户名","收获地址","创建时间")+"</td>");
				for(Map<String,Object> map :list){
					sb.append("<tr>"+setTd(map.get("user_id"),map.get("contacts_address"),map.get("ctime"))+"</td>");
				}
				sb.append("</table>");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		super.responseSendMsg(response, sb.toString(),"text/html");
	}
	
	/**
	 * 获取用户积分详情
	 * @author wtj
	 * @date 2016年2月1日下午5:16:37
	 * @param request
	 * @param response
	 * @param value
	 * @throws IOException
	 */
	@RequestMapping(value = "load_user_point_history", method = {RequestMethod.GET,RequestMethod.POST })
	public void load_user_point_history(HttpServletRequest request,HttpServletResponse response,String value)throws IOException {
		super.requestLog("load_user_point_history",value);
		StringBuffer sb = new StringBuffer();
		try {
			@SuppressWarnings("unchecked")
			Map<String, String> param = JSON.parseObject(value, HashMap.class);
			String loginId = param.get("loginId");
			String loginPassword = param.get("loginPassword");
			String userId = param.get("userId");
			String from = param.get("from");
			String to = param.get("to");
			ParamUtil.notNullParam("loginId:"+loginId,"loginPassword:"+loginPassword);
			
			if(!isShow(loginId,loginPassword)){
				sb.append(NO_SHOW_MSG);
			}else{
				String where  = "";
				if(StringUtils.isNotBlank(userId)){
					where =" and user_id = '"+userId+"'";
				}
				if(StringUtils.isBlank(from)){
					from = "2000-01-01";
				}
				if(StringUtils.isBlank(to)){
					to = "3000-01-01";
				}
				String sql = "select user_id,point,remark,time,type_info,operate_type from user_point_history where 1=1 "+where+" and time BETWEEN '"+from+"'  and  '"+to+"'  ORDER BY  time desc";
				List<Map<String,Object>> list = bt.queryListMap(sql);
				sb.append("<table width='100%'>");
				sb.append("<tr>"+setTd("用户名","积分","时间","说明")+"</td>");
				for(Map<String,Object> map :list){
					sb.append("<tr>"+setTd(map.get("user_id"),map.get("point"),map.get("time"),map.get("type_info"))+"</td>");
				}
				sb.append("</table>");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		super.responseSendMsg(response, sb.toString(),"text/html");
	}
	
	/**
	 * 线下见面会报名情况详情
	 * @author wtj
	 * @date 2016年2月26日下午12:12:20
	 * @param request
	 * @param response
	 * @param value
	 * @throws IOException
	 */
	@RequestMapping(value = "ua_list", method = {RequestMethod.GET,RequestMethod.POST })
	public void ua_list(HttpServletRequest request,HttpServletResponse response,String value)throws IOException {
		super.requestLog("ua_list",value);
		JSONObject json = new JSONObject();
		StringBuffer sb = new StringBuffer();
		try {
			@SuppressWarnings("unchecked")
			Map<String, String> param = JSON.parseObject(value, HashMap.class);
			String loginId = param.get("loginId");
			String loginPassword = param.get("loginPassword");
			String type = param.get("type");
			ParamUtil.notNullParam("loginId:"+loginId,"loginPassword:"+loginPassword);
			
			if(!isShow(loginId,loginPassword)){
				sb.append(NO_SHOW_MSG);
			}else{
				String where  = "";
				if(StringUtils.isNotBlank(type)){
					where =" and type='"+type+"'";
				}
				String sql = "select us.id as user_id ,us.realname ,us.sex,us.mobile_number,us.id_card,ua.apply_time,ua.status,invest_money as thatInvestMoney from user_activity ua INNER JOIN `user` us on ua.user_id = us.id "+where+" ORDER BY ua.apply_time desc";
				List<Map<String,Object>> list = bt.queryListMap(sql);
				sb.append("<table width='100%'>");
				sb.append("<tr>"+setTd("用户名","姓名","性别","注册电话","身份证号码","申请时间","申请时待收金额","当前待收金额","状态")+"</tr>");
				List<Map<String,Object>> investMoney = new ArrayList<Map<String,Object>>();
				Object im = "";
				for(Map<String,Object> map :list){
					investMoney = bt.queryListMap("select sum(inv.invest_money) investMoney from invest inv "
							+ "inner join loan lo on inv.loan_id = lo.id "
							+ "inner join loan_type lt on lo.type = lt.id and lt.repay_time_unit!='day' "
							+ "where inv.`status` = 'repaying' and inv.user_id = '"+map.get("user_id")+"' and lo.deadline != 8 ");
					if(investMoney.size() != 0 && investMoney.get(0)!=null){
						im = investMoney.get(0).get("investMoney");
					}else{
						im = "--";
					}
					String sex = map.get("sex")==null?"":map.get("sex").equals("F")?"女":"男";
					sb.append("<tr>"+setTd(map.get("user_id"),map.get("realname"),sex,map.get("mobile_number"),map.get("id_card"),map.get("apply_time"),map.get("thatInvestMoney"),im,loanXXBMStatus(map.get("status").toString()))+"</tr>");
				}
				sb.append("</table>");
			}
		} catch(CxdException e){
			super.cxdJsonException(json, e,value);
			sb.append(json.toString());
		}catch (Exception e) {
			super.jsonException(json, e, logger);
			sb.append(json.toString());
		}
		super.responseSendMsg(response, sb.toString(),"text/html");
	}
	
	private String loanXXBMStatus(String status){
		if(status.equals("1")){
			return "报名成功";
		}else if(status.equals("2")){
			return "不符合规则";
		}else if(status.equals("3")){
			return "参加过往届";
		}else if(status.equals("4")){
			return "正在审核中";
		}else{
			return "";
		}
	}
	/**
	 * 获取用户绑卡信息
	 * @author wtj
	 * @date 2016年2月26日下午12:02:17
	 * @param request
	 * @param response
	 * @param value
	 * @throws IOException
	 */
	@RequestMapping(value = "load_user_bind_card", method = {RequestMethod.GET,RequestMethod.POST })
	public void load_user_bind_card(HttpServletRequest request,HttpServletResponse response,String value)throws IOException {
		super.requestLog("load_user_bind_card",value);
		JSONObject json = new JSONObject();
		StringBuffer sb = new StringBuffer();
		try {
			@SuppressWarnings("unchecked")
			Map<String, String> param = JSON.parseObject(value, HashMap.class);
			String loginId = param.get("loginId");
			String loginPassword = param.get("loginPassword");
			ParamUtil.notNullParam("loginId:"+loginId,"loginPassword:"+loginPassword);
			
			if(!isShow(loginId,loginPassword)){
				sb.append(NO_SHOW_MSG);
			}else{
				String sql = "select user_id,bc.bank,us.mobile_number,bc.time  from bank_card bc inner join `user` us on bc.user_id = us.id where bc.`status` != '已删除' and bc.`status` != 'FAIL' ORDER BY time desc";
				List<Map<String,Object>> list = bt.queryListMap(sql);
				sb.append("<table width='100%'>");
				sb.append("<tr>"+setTd("用户名","开卡行","手机号","绑卡时间")+"</tr>");
				for(Map<String,Object> map :list){
					sb.append("<tr>"+setTd(map.get("user_id"),map.get("bank"),map.get("mobile_number"),map.get("time"))+"</tr>");
				}
				sb.append("</table>");
			}
		} catch(CxdException e){
			super.cxdJsonException(json, e,value);
			sb.append(json.toString());
		}catch (Exception e) {
			super.jsonException(json, e, logger);
			sb.append(json.toString());
		}
		super.responseSendMsg(response, sb.toString(),"text/html");
	}
	
	/**
	 * 获取本月投资top30
	 * @param request
	 * @param response
	 * @param value
	 * @throws IOException
	 */
	@RequestMapping(value = "invest_top30", method = {RequestMethod.GET,RequestMethod.POST })
	public void user_invest_top30(HttpServletRequest request,HttpServletResponse response,String value)throws IOException {
		super.requestLog("invest_top30",value);
		StringBuffer sb = new StringBuffer();
		try {
			@SuppressWarnings("unchecked")
			Map<String, String> param = JSON.parseObject(value, HashMap.class);
			
			String loginId = param.get("loginId");
			String loginPassword = param.get("loginPassword");
			
			ParamUtil.notNullParam("loginId:"+loginId,"loginPassword:"+loginPassword);
			
			if(!isShow(loginId,loginPassword)){
				sb.append(NO_SHOW_MSG);
			}else{
				String month = param.get("month");
				String sql = "select t.user_id as user,u.username,u.realname,if(u.sex='M','男','女') sex,u.mobile_number,sum(t.money) as tmoney from invest t join user u on t.user_id=u.id  "
						+ "where  date_format(t.time, '%Y%m') = date_format(curdate(),'%Y%m') and t.status<>'cancel' group by t.user_id order by sum(t.money) desc limit 30 ";
				
				if(StringUtils.isNotEmpty(month)){
					sql = "select t.user_id as user,u.username,u.realname,if(u.sex='M','男','女') sex,u.mobile_number,sum(t.money) as tmoney from invest t join user u on t.user_id=u.id  "
							+ "where  date_format(t.time, '%Y%m') = '"+month+"' and t.status<>'cancel' group by t.user_id order by sum(t.money) desc limit 30 ";
				}
				List<Map<String,Object>> list = bt.queryListMap(sql);
				sb.append("<table width='100%'>");
				sb.append("<tr>"+setTd("用户ID","用户名","姓名","性别","手机","本月投资总额")+"</td>");
				for(Map<String,Object> map :list){
					sb.append("<tr>"+setTd(map.get("user"),map.get("username"),map.get("realname"),map.get("sex"),map.get("mobile_number"),NumberUtil.getNumber((Double) map.get("tmoney")))+"</td>");
				}
				sb.append("</table>");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		super.responseSendMsg(response, sb.toString(),"text/html");
	}
	
	
	/**
	 * 项目投资明细表
	 * @author wtj
	 * @date 2016年3月25日下午1:09:08
	 * @param request
	 * @param response
	 * @param value
	 * @throws IOException
	 */
	@RequestMapping(value = "load_loan_invest_detail", method = {RequestMethod.GET,RequestMethod.POST })
	public void load_loan_invest_detail(HttpServletRequest request,HttpServletResponse response,String value)throws IOException {
		super.requestLog("load_loan_invest_detail",value);
		JSONObject json = new JSONObject();
		StringBuffer sb = new StringBuffer();
		try {
			@SuppressWarnings("unchecked")
			Map<String, String> param = JSON.parseObject(value, HashMap.class);
			String loginId = param.get("loginId");
			String loginPassword = param.get("loginPassword");
			ParamUtil.notNullParam("loginId:"+loginId,"loginPassword:"+loginPassword);
			String where = "";
			// 借款ID
			String loanId = param.get("loanId");
			if(StringUtils.isNotBlank(loanId)){
				where = String.format(" and l.id ='%s'",loanId);
			}
			if(!isShow(loginId,loginPassword)){
				sb.append(NO_SHOW_MSG);
			}else{
				String sql = "select l.id,l.name,l.type,u.username ,u.referrer,i.time,i.is_auto_invest,i.money,i.status,i.id as invest_id,l.rate,l.deadline,i.user_id "
							+ "from  loan l "
							+ "LEFT JOIN `user` u on l.user_id=u.id "
							+ "LEFT JOIN invest i on l.id=i.loan_id "
							+ " where 1=1 "+where
							+ "ORDER BY l.commit_time desc";
				List<Map<String,Object>> list = bt.queryListMap(sql);
				sb.append("<table width='100%'>");
				
				sb.append("<tr>"+setTd("借款人","投资人","投资标识","投资时间","投资金额","投标状态","项目编号","项目名称","项目类型","项目利率","项目期限")+"</tr>");
				for(Map<String,Object> map :list){
					sb.append("<tr>"+setTd(map.get("username"),map.get("user_id"),map.get("invest_id"),map.get("time"),map.get("money"),PublicConstants.investStatusMap.get(map.get("status")),map.get("id"),map.get("name"),PublicConstants.loanType.get(map.get("type")),(map.get("deadline")+PublicConstants.loanTimeType.get(map.get("type")).toString()),map.get("rate"))+"</tr>");
				}
				sb.append("</table>");
			}
		} catch(CxdException e){
			super.cxdJsonException(json, e,value);
			sb.append(json.toString());
		}catch (Exception e) {
			super.jsonException(json, e, logger);
			sb.append(json.toString());
		}
		super.responseSendMsg(response, sb.toString(),"text/html");
	}
	
	/**
	 * 用户账户资金
	 * @author wtj
	 * @date 2016年3月25日下午1:10:32
	 * @param request
	 * @param response
	 * @param value
	 * @throws IOException
	 */
	@RequestMapping(value = "user_bill", method = {RequestMethod.GET,RequestMethod.POST })
	public void user_bill(HttpServletRequest request,HttpServletResponse response,String value)throws IOException {
		super.requestLog("user_bill",value);
		JSONObject json = new JSONObject();
		StringBuffer sb = new StringBuffer();
		try {
			@SuppressWarnings("unchecked")
			Map<String, String> param = JSON.parseObject(value, HashMap.class);
			String loginId = param.get("loginId");
			String loginPassword = param.get("loginPassword");
			ParamUtil.notNullParam("loginId:"+loginId,"loginPassword:"+loginPassword);
			String where = "";
			
			// 根据用户id查询
			String userId = param.get("userId");
			if(StringUtils.isNotBlank(userId)){
				where = String.format(" and u.id ='%s'",userId);
			}
			
			String limit;
			String max = param.get("max");
			String min = param.get("min");
			
			if(StringUtils.isBlank(max)||StringUtils.isBlank(min)){
				json.put("msg", "分页信息不完整");
				sb.append(json.toString());
			}else{
				limit = String.format(" limit %s,%s",max,min);
				if(!isShow(loginId,loginPassword)){
					sb.append(NO_SHOW_MSG);
				}else{
					String sql = " select b.time ,b.seq_num ,u.username ,b.type ,b.type_info , b.money ,b.balance ,b.frozen_money ,b.detail  "
							+ "from user_bill b  LEFT JOIN `user` as u on b.user_id=u.id "
							+ " where 1=1 "+where
							+ "ORDER BY b.time desc"+limit;
					List<Map<String,Object>> list = bt.queryListMap(sql);
					sb.append("<table width='100%'>");
					
					sb.append("<tr>"+setTd("用户名","操作类型","费用类型","时间","金额","余额","冻结金额","费用详情")+"</tr>");
					for(Map<String,Object> map :list){
						sb.append("<tr>"+setTd(map.get("username"),PublicConstants.moneyOperationType.get(map.get("type_info")),PublicConstants.userBillType.get(map.get("type")),map.get("time"),map.get("money"),map.get("balance"),map.get("frozen_money"),map.get("detail"))+"</tr>");
					}
					sb.append("</table>");
				}
			}
		} catch(CxdException e){
			super.cxdJsonException(json, e,value);
			sb.append(json.toString());
		}catch (Exception e) {
			super.jsonException(json, e, logger);
			sb.append(json.toString());
		}
		super.responseSendMsg(response, sb.toString(),"text/html");
	}
	
	/**
	 * 充值记录表
	 * @author wtj
	 * @date 2016年3月25日下午2:44:13
	 * @param request
	 * @param response
	 * @param value
	 * @throws IOException
	 */
	@RequestMapping(value = "recharge_detail", method = {RequestMethod.GET,RequestMethod.POST })
	public void recharge_detail(HttpServletRequest request,HttpServletResponse response,String value)throws IOException {
		super.requestLog("recharge_detail",value);
		JSONObject json = new JSONObject();
		StringBuffer sb = new StringBuffer();
		try {
			@SuppressWarnings("unchecked")
			Map<String, String> param = JSON.parseObject(value, HashMap.class);
			String loginId = param.get("loginId");
			String loginPassword = param.get("loginPassword");
			ParamUtil.notNullParam("loginId:"+loginId,"loginPassword:"+loginPassword);
			String where = "";
			
			// 根据用户id查询
			String userId = param.get("userId");
			if(StringUtils.isNotBlank(userId)){
				where = String.format(" and u.id ='%s'",userId);
			}
			
			String limit;
			String max = param.get("max");
			String min = param.get("min");
			
			if(StringUtils.isBlank(max)||StringUtils.isBlank(min)){
				json.put("msg", "分页信息不完整");
				sb.append(json.toString());
			}else{
				limit = String.format(" limit %s,%s",max,min);
				if(!isShow(loginId,loginPassword)){
					sb.append(NO_SHOW_MSG);
				}else{
					String sql = " select r.id,r.time,u.username,r.actual_money,r.fee ,r.recharge_way, "
							+ " r.is_recharged_by_admin ,r.status from recharge r left JOIN `user` u ON r.user_id=u.id where 1=1 "+where +" ORDER BY time desc "+limit;
					List<Map<String,Object>> list = bt.queryListMap(sql);
					sb.append("<table width='100%'>");
					
					sb.append("<tr>"+setTd("用户名","编号","充值时间","金额","管理员充值","充值方式","状态")+"</tr>");
					for(Map<String,Object> map :list){
						sb.append("<tr>"+setTd(map.get("username"),map.get("id"),map.get("time"),map.get("actual_money"),PublicConstants.rechargeAdminMap.get(map.get("is_recharged_by_admin")) ,map.get("recharge_way"),PublicConstants.rechargeStatusMap.get(map.get("status")))+"</tr>");
					}
					sb.append("</table>");
				}
			}
		} catch(CxdException e){
			super.cxdJsonException(json, e,value);
			sb.append(json.toString());
		}catch (Exception e) {
			super.jsonException(json, e, logger);
			sb.append(json.toString());
		}
		super.responseSendMsg(response, sb.toString(),"text/html");
	}
	
	/**
	 * 获取用户信息
	 * @author wtj
	 * @date 2016年3月28日下午5:37:57
	 * @param request
	 * @param response
	 * @param value
	 * @throws IOException
	 */
	@RequestMapping(value = "load_user_info", method = {RequestMethod.GET,RequestMethod.POST })
	public void load_user_info(HttpServletRequest request,HttpServletResponse response,String value)throws IOException {
		super.requestLog("load_user_info",value);
		JSONObject json = new JSONObject();
		StringBuffer sb = new StringBuffer();
		try {
			@SuppressWarnings("unchecked")
			Map<String, String> param = JSON.parseObject(value, HashMap.class);
			String loginId = param.get("loginId");
			String loginPassword = param.get("loginPassword");
			ParamUtil.notNullParam("loginId:"+loginId,"loginPassword:"+loginPassword);
			String where = "";
			
			// 根据用户id查询
			String userId = param.get("userId");
			if(StringUtils.isNotBlank(userId)){
				where = String.format(" and us.id ='%s'",userId);
			}
			
			String limit;
			String max = param.get("max");
			String min = param.get("min");
			
			if(StringUtils.isBlank(max)||StringUtils.isBlank(min)){
				json.put("msg", "分页信息不完整");
				sb.append(json.toString());
			}else{
				limit = String.format(" limit %s,%s",max,min);
				if(!isShow(loginId,loginPassword)){
					sb.append(NO_SHOW_MSG);
				}else{
					String sql = "select us.id,us.realname ,us.sex,us.birthday,us.mobile_number,us.email,us.register_time,ta.create_time reg_time,us.`status`,us.source_name ,us.source_type ,us.referrer "
							+ "from `user` us  LEFT JOIN trusteeship_account ta on us.id = ta.user_id  where 1=1 " +where+ " ORDER BY us.register_time desc " +limit;
					List<Map<String,Object>> list = bt.queryListMap(sql);
					sb.append("<table width='100%'>");
					
					sb.append("<tr>"+setTd("用户名","姓名","性别","年龄","生日","手机号码","电子邮件","注册时间","开通第三方时间","状态","推荐人","渠道","客户端")+"</tr>");
					for(Map<String,Object> map :list){
						sb.append("<tr>"+setTd(map.get("id"),map.get("realname"),PublicConstants.sexType.get(map.get("sex")),
								PublicConstants.getAge(map.get("birthday")),map.get("birthday") ,map.get("mobile_number"),map.get("email"),map.get("register_time"),
								map.get("reg_time"),"可用",map.get("referrer"),map.get("source_name"),PublicConstants.getSourceTypeName(map.get("source_type")))+"</tr>");
					}
					sb.append("</table>");
				}
			}
		} catch(CxdException e){
			super.cxdJsonException(json, e,value);
			sb.append(json.toString());
		}catch (Exception e) {
			super.jsonException(json, e, logger);
			sb.append(json.toString());
		}
		super.responseSendMsg(response, sb.toString(),"text/html");
	}
	
	/**
	 * 投资分析表字段
	 * @author wtj
	 * @date 2016年3月29日下午3:22:04
	 * @param request
	 * @param response
	 * @param value
	 * @throws IOException
	 */
	@RequestMapping(value = "invest_statistics", method = {RequestMethod.GET,RequestMethod.POST })
	public void invest_statistics(HttpServletRequest request,HttpServletResponse response,String value)throws IOException {
		super.requestLog("invest_statistics",value);
		JSONObject json = new JSONObject();
		StringBuffer sb = new StringBuffer();
		try {
			@SuppressWarnings("unchecked")
			Map<String, String> param = JSON.parseObject(value, HashMap.class);
			String loginId = param.get("loginId");
			String loginPassword = param.get("loginPassword");
			ParamUtil.notNullParam("loginId:"+loginId,"loginPassword:"+loginPassword);
			String where = "";
			
			// 根据用户id查询
			String userId = param.get("userId");
			if(StringUtils.isNotBlank(userId)){
				where = String.format(" and user_id ='%s'",userId);
			}
			
			String limit;
			String max = param.get("max");
			String min = param.get("min");
			
			if(StringUtils.isBlank(max)||StringUtils.isBlank(min)){
				json.put("msg", "分页信息不完整");
				sb.append(json.toString());
			}else{
				limit = String.format(" limit %s,%s",max,min);
				if(!isShow(loginId,loginPassword)){
					sb.append(NO_SHOW_MSG);
				}else{
					String sql = "select DISTINCT user_id userId from invest where 1=1 "+where +limit;
					List<Map<String,Object>> list = bt.queryListMap(sql);
					sb.append("<table width='100%'>");
					sb.append("<tr>"+setTd("用户名","余额","在投金额","累投金额","首次投资","投资次数","待收利息","已收利息")+"</tr>");
					// 用户余额
					Map<String,Object>  userBalance = getUserBalance();
					// 在投金额
					Map<String,Object>  investIngMoney = getUserInvestIngMoney();
					// 累投金额
					Map<String,Object>  totalInvestMoney = getUserTotalInvestMoney();
					// 首次投资金额
					Map<String,Object>  firstInvestMoney = getFirstInvestMoney();
					// 投资次数
					Map<String,Object>  totalInvestCount = getUserTotalInvestCount();
					// 待收利息
					Map<String,Object>  willCollectInterest = getWillCollectInterest();
					// 已收利息
					Map<String,Object>  collectedInterest = getCollectedInterest();
					
					for(Map<String,Object> map :list){
						String ui = map.get("userId").toString();
						sb.append("<tr>"+setTd(ui,userBalance.get(ui),investIngMoney.get(ui),
								totalInvestMoney.get(ui),firstInvestMoney.get(ui) ,
								totalInvestCount.get(ui),willCollectInterest.get(ui),
								collectedInterest.get(ui))+"</tr>");
					}
					sb.append("</table>");
				}
			}
		} catch(CxdException e){
			super.cxdJsonException(json, e,value);
			sb.append(json.toString());
		}catch (Exception e) {
			super.jsonException(json, e, logger);
			sb.append(json.toString());
		}
		super.responseSendMsg(response, sb.toString(),"text/html");
	}
	
	/**
	 * 投资明细
	 * <一句话功能简述> <功能详细描述>
	 * @author wtj
	 * @date 2016年3月30日上午10:59:45
	 * @param request
	 * @param response
	 * @param value
	 * @throws IOException
	 */
	@RequestMapping(value = "invest_info", method = {RequestMethod.GET,RequestMethod.POST })
	public void invest_info(HttpServletRequest request,HttpServletResponse response,String value)throws IOException {
		super.requestLog("invest_info",value);
		JSONObject json = new JSONObject();
		StringBuffer sb = new StringBuffer();
		try {
			@SuppressWarnings("unchecked")
			Map<String, String> param = JSON.parseObject(value, HashMap.class);
			String loginId = param.get("loginId");
			String loginPassword = param.get("loginPassword");
			ParamUtil.notNullParam("loginId:"+loginId,"loginPassword:"+loginPassword);
			String where = "";
			
			// 根据用户id查询
			String userId = param.get("userId");
			if(StringUtils.isNotBlank(userId)){
				where = String.format(" and inv.user_id ='%s'",userId);
			}
			
			String limit;
			String max = param.get("max");
			String min = param.get("min");
			
			if(StringUtils.isBlank(max)||StringUtils.isBlank(min)){
				json.put("msg", "分页信息不完整");
				sb.append(json.toString());
			}else{
				limit = String.format(" limit %s,%s",max,min);
				if(!isShow(loginId,loginPassword)){
					sb.append(NO_SHOW_MSG);
				}else{
					String sql = "select inv.user_id userId,lo.id loanId,inv.id investId,lo.`name`,lo.type,inv.money,lo.rate,lo.deadline,inv.time investTime from invest inv LEFT JOIN loan lo on inv.loan_id = lo.id where inv.`status` not in ('cancel') "+where+" ORDER BY inv.time desc " +limit;
					List<Map<String,Object>> list = bt.queryListMap(sql);
					sb.append("<table width='100%'>");
					sb.append("<tr>"+setTd("用户名","借款项目ID","投资项目ID","项目名称","项目类型","投资金额","利息收入","年化利率","项目期限","投标时间")+"</tr>");
					
					// 投资利息
					Map<String,Object>  investInterest = getInvestInterest();
					for(Map<String,Object> map :list){
						String ui = map.get("userId").toString();
						sb.append("<tr>"+setTd(ui,map.get("loanId"),map.get("investId"),map.get("name"),PublicConstants.loanType.get(map.get("type")),
								map.get("money"),investInterest.get(map.get("investId")),map.get("rate"),(getLoanDay(map.get("deadline").toString(),map.get("type").toString())),
								map.get("investTime"))+"</tr>");
					}
					sb.append("</table>");
				}
			}
		} catch(CxdException e){
			super.cxdJsonException(json, e,value);
			sb.append(json.toString());
		}catch (Exception e) {
			super.jsonException(json, e, logger);
			sb.append(json.toString());
		}
		super.responseSendMsg(response, sb.toString(),"text/html");
	}
	
	/**
	 * 投资回款明细
	 * @author wtj
	 * @date 2016年3月30日上午10:59:45
	 * @param request
	 * @param response
	 * @param value
	 * @throws IOException
	 */
	@RequestMapping(value = "invest_repay_info", method = {RequestMethod.GET,RequestMethod.POST })
	public void invest_repay_info(HttpServletRequest request,HttpServletResponse response,String value)throws IOException {
		super.requestLog("invest_repay_info",value);
		JSONObject json = new JSONObject();
		StringBuffer sb = new StringBuffer();
		try {
			@SuppressWarnings("unchecked")
			Map<String, String> param = JSON.parseObject(value, HashMap.class);
			String loginId = param.get("loginId");
			String loginPassword = param.get("loginPassword");
			ParamUtil.notNullParam("loginId:"+loginId,"loginPassword:"+loginPassword);
			String where = "";
			
			// 根据用户id查询
			String userId = param.get("userId");
			if(StringUtils.isNotBlank(userId)){
				where = String.format(" and inv.user_id ='%s'",userId);
			}
			
			String limit;
			String max = param.get("max");
			String min = param.get("min");
			
			if(StringUtils.isBlank(max)||StringUtils.isBlank(min)){
				json.put("msg", "分页信息不完整");
				sb.append(json.toString());
			}else{
				limit = String.format(" limit %s,%s",max,min);
				if(!isShow(loginId,loginPassword)){
					sb.append(NO_SHOW_MSG);
				}else{
					String sql = "select inv.user_id userId,lo.id loanId,inv.id investId,lo.`name`,lo.type,lo.deadline,lo.rate,inv.money from invest inv LEFT JOIN loan lo on inv.loan_id = lo.id where inv.`status` not in ('cancel') "+where+" ORDER BY inv.time desc  " +limit;
					List<Map<String,Object>> list = bt.queryListMap(sql);
					sb.append("<table width='100%'>");
					sb.append("<tr>"+setTd("回款日期","用户名","借款项目ID","投资项目ID","项目名称","应还本金","应还利息","年化利率","项目期限","应还总额")+"</tr>");
					
					// 投资利息
					Map<String,Object>  investInterest = getInvestInterest();
					Map<String,Object>  investLastRepayTime = getInvestLastRepayTime();
					for(Map<String,Object> map :list){
						String ui = map.get("userId").toString();
						Double investMoney = Double.parseDouble(map.get("money")==null?"0":map.get("money").toString());
						Double nterestMoney = Double.parseDouble(investInterest.get(map.get("investId"))==null?"0":investInterest.get(map.get("investId")).toString());
						
						sb.append("<tr>"+setTd(investLastRepayTime.get(map.get("investId")),ui,map.get("loanId"),map.get("investId"),map.get("name"),investMoney,
								nterestMoney,getRate(map.get("rate").toString()),getLoanDay(map.get("deadline").toString(),map.get("type").toString()),
								ArithUtil.add(investMoney, nterestMoney)
								)+"</tr>");
					}
					sb.append("</table>");
				}
			}
		} catch(CxdException e){
			super.cxdJsonException(json, e,value);
			sb.append(json.toString());
		}catch (Exception e) {
			super.jsonException(json, e, logger);
			sb.append(json.toString());
		}
		super.responseSendMsg(response, sb.toString(),"text/html");
	}
	public String getRate(String rate){
		return (ArithUtil.mul(Double.parseDouble(rate),100d)+"%");
	}
	/**
	 * 获取用户投资中总额
	 * @author wtj
	 * @date 2016年3月30日上午9:03:32
	 * @return
	 */
	public Map<String,Object> getUserInvestIngMoney(){
		Map<String,Object> map = new HashMap<String,Object>();
		List<Map<String,Object>> listMap = bt.queryListMap("select inv.user_id as userId,sum(inv.money) investIngMoney from invest inv where inv.`status` not in ('cancel','complete') GROUP BY inv.user_id");
		for(Map<String,Object> m:listMap){
			map.put(m.get("userId").toString(), m.get("investIngMoney"));
		}
		return map;
	}
	
	/**
	 * 获取借款天数
	 * @author wtj
	 * @date 2016年3月31日上午9:05:03
	 * @return
	 */
	public int getLoanDay(String deadline,String type){
		if(PublicConstants.loanTimeType.get(type).equals("天")){
			return Integer.parseInt(deadline);
		}else{
			return Integer.parseInt(deadline)*30;
		}
	}
	/**
	 * 用户总投资金额
	 * @author wtj
	 * @date 2016年3月30日上午9:03:32
	 * @return
	 */
	public Map<String,Object> getUserTotalInvestMoney(){
		Map<String,Object> map = new HashMap<String,Object>();
		List<Map<String,Object>> listMap = bt.queryListMap("select inv.user_id as userId,sum(inv.money) totalInvestMoney from invest inv where inv.`status` not in ('cancel') GROUP BY inv.user_id");
		for(Map<String,Object> m:listMap){
			map.put(m.get("userId").toString(), m.get("totalInvestMoney"));
		}
		return map;
	}
	
	/**
	 *用户总投资次数
	 */
	public Map<String,Object> getUserTotalInvestCount(){
		Map<String,Object> map = new HashMap<String,Object>();
		List<Map<String,Object>> listMap = bt.queryListMap("select inv.user_id userId,count(inv.money) totalInvestCount from invest inv where inv.`status` not in ('cancel') GROUP BY inv.user_id");
		for(Map<String,Object> m:listMap){
			map.put(m.get("userId").toString(), m.get("totalInvestCount"));
		}
		return map;
	}
	/**
	 * 用户待收利息
	 * @author wtj
	 * @date 2016年3月30日上午9:20:56
	 * @return
	 */
	public Map<String,Object> getWillCollectInterest(){
		Map<String,Object> map = new HashMap<String,Object>();
		List<Map<String,Object>> listMap = bt.queryListMap("select inv.user_id userId , sum(ir.interest) willCollectInterest from invest inv inner join invest_repay ir on inv.id = ir.invest_id where  ir.status = 'repaying'  GROUP BY inv.user_id");
		for(Map<String,Object> m:listMap){
			map.put(m.get("userId").toString(), m.get("willCollectInterest"));
		}
		return map;
	}
	/**
	 * 用户待已收利息
	 * @author wtj
	 * @date 2016年3月30日上午9:20:56
	 * @return
	 */
	public Map<String,Object> getCollectedInterest(){
		Map<String,Object> map = new HashMap<String,Object>();
		List<Map<String,Object>> listMap = bt.queryListMap("select inv.user_id userId , sum(ir.interest) collectedInterest from invest inv inner join invest_repay ir on inv.id = ir.invest_id where  ir.status = 'complete'  GROUP BY inv.user_id");
		for(Map<String,Object> m:listMap){
			map.put(m.get("userId").toString(), m.get("collectedInterest"));
		}
		return map;
	} 
	
	/**
	 * 首次投资金额
	 * @author wtj
	 * @date 2016年3月30日上午9:41:41
	 * @return
	 */
	public Map<String,Object> getFirstInvestMoney(){
		Map<String,Object> map = new HashMap<String,Object>();
		List<Map<String,Object>> listMap = bt.queryListMap("select inv.user_id as userId,money as firstInvestMoney from invest inv inner join (select user_id,min(time) time from invest inv where inv.status not in ('cancel') GROUP BY user_id) t1 on inv.user_id = t1.user_id and inv.time = t1.time");
		for(Map<String,Object> m:listMap){
			map.put(m.get("userId").toString(), m.get("firstInvestMoney"));
		}
		return map;
	} 
	
	/**
	 * 获取用户余额
	 * @author wtj
	 * @date 2016年3月30日上午9:54:40
	 * @return
	 */
	public Map<String,Object> getUserBalance(){
		Map<String,Object> map = new HashMap<String,Object>();
		List<Map<String,Object>> listMap = bt.queryListMap("select DISTINCT ub.user_id userId,ub.balance from user_bill ub inner join (select user_id ,max(seq_num) seq_num from user_bill GROUP BY user_id) t1 on ub.user_id = t1.user_id and ub.seq_num=t1.seq_num");
		for(Map<String,Object> m:listMap){
			map.put(m.get("userId").toString(), m.get("balance"));
		}
		return map;
	} 
	
	/**
	 * 根据投资ID 获取投资利息
	 * @author wtj
	 * @date 2016年3月30日上午11:16:18
	 * @return
	 */
	public Map<String,Object> getInvestInterest(){
		Map<String,Object> map = new HashMap<String,Object>();
		List<Map<String,Object>> listMap = bt.queryListMap("select invest_id as investId ,sum(ir.interest) interest from invest_repay ir GROUP BY ir.invest_id");
		for(Map<String,Object> m:listMap){
			map.put(m.get("investId").toString(), m.get("interest"));
		}
		return map;
	} 
	
	/**
	 * 根据投资ID 获取投资最后还款时间
	 * @author wtj
	 * @date 2016年3月30日上午11:16:18
	 * @return
	 */
	public Map<String,Object> getInvestLastRepayTime(){
		Map<String,Object> map = new HashMap<String,Object>();
		List<Map<String,Object>> listMap = bt.queryListMap("select ir.invest_id as investId,ir.repay_day as repayDay from invest_repay ir inner join (select invest_id,max(repay_day) repay_day from invest_repay GROUP BY invest_id ) t1 on ir.invest_id = t1.invest_id and ir.repay_day = t1.repay_day");
		for(Map<String,Object> m:listMap){
			map.put(m.get("investId").toString(), m.get("repayDay"));
		}
		return map;
	}
	
	
	/**
	 * 拼装td
	 * @author wtj
	 * @date 2016年2月1日下午4:40:16
	 * @param tds
	 * @return
	 */
	public String setTd(Object... tds){
		StringBuffer sb = new StringBuffer();
		for(Object td:tds){
			sb.append("<td>"+td+"</td>");
		}
		return sb.toString();
	}
	
	
	/**
	 * 用户是否有查看报表的权利
	 * @author wtj
	 * @date 2016年2月1日下午4:44:11
	 * @param userId
	 * @return
	 */
	public Boolean isShow(String userId,String loginPassword){
		UserBase user = bt.queryBean("select * from user where (id='"+userId+"' or mobile_number='"+userId+"') and password='"+HashCrypt.getDigestHash(loginPassword.toString())+"'", UserBase.class);
		if(user ==null){
			return false;
		}
		List<String> list = new ArrayList<String>();
		list.add("chenyuan1974");
		list.add("michael");
		list.add("bibeibei");
		list.add("1030532998");
		list.add("wlsnow");
		if(list.contains(userId.toLowerCase())){
			return true;
		}else{
			return false;
		}
	}
	/**
	 * 短信投资信息
	 * @param request
	 * @param response
	 * @param pageNum
	 * @param pagesize
	 * @throws IOException
	 */
	@RequestMapping(value = "sms_invest_info", method = {RequestMethod.GET,RequestMethod.POST })
	public void sms_invset_info(HttpServletRequest request,HttpServletResponse response,String min,String max)throws IOException {
		StringBuffer sb = new StringBuffer();
		String limit="";
		if(StringUtils.isNotBlank(min) && StringUtils.isNotBlank(max)){
			limit = String.format(" limit %s,%s",min,max);	
		}
		String sql = "select id,ctime,phone_no,info,balance,last_invest_day from sms_invest_info where phone_no is not null and phone_no != '' ORDER BY ctime desc    " +limit;
		List<Map<String,Object>> list = bt.queryListMap(sql);
		sb.append("<table width='100%'>");
		sb.append("<tr>"+setTd("发送时间","手机号","发送信息","余额","最后投资日期")+"</tr>");
		if(list!=null&&list.size()>0){
			for(Map<String,Object> map :list){
				sb.append("<tr>"+setTd(map.get("ctime"),map.get("phone_no"),map.get("info"),
						map.get("balance"),map.get("last_invest_day"))
						+"</tr>");
				
			}
		}
		sb.append("</table>");
		super.responseSendMsg(response, sb.toString(),"text/html");
	}	
	/**
	 * 墨积分用户统计
	 * @author wtj
	 * @date 2016年4月20日下午2:25:27
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping(value = "referrer_users", method = {RequestMethod.GET,RequestMethod.POST })
	public void referrer_users(HttpServletResponse response,String value)throws IOException {
		super.requestLog("getcode",value);
		JSONObject json = new JSONObject();
		StringBuffer sb = new StringBuffer();
		try {
			@SuppressWarnings("unchecked")
			Map<String, String> param = JSON.parseObject(value, HashMap.class);
			String loginId = param.get("loginId");
			String loginPassword = param.get("loginPassword");
			String byUserId = param.get("byUserId");
			ParamUtil.notNullParam("loginId:"+loginId,"loginPassword:"+loginPassword);
			if(!isShow(loginId,loginPassword)){
				json.put("msg",NO_SHOW_MSG);
				json.put("status","500");
			}else{
				String sql = "select user_id,sum(money) money,u.register_time,u.mobile_number "
						+ "from invest t join user u on t.user_id = u.id join user ru  on u.referrer = ru.id "
						+ "where ru.id='%s' and t.status != 'cancel' group by t.user_id order by u.register_time desc";
				List<Map<String,Object>> list = bt.queryListMap(String.format(sql,byUserId));
				sb.append("<table width='100%'>");
				sb.append("<tr>"+setTd("用户ID","投资金额","注册时间","手机号")+"</tr>");
				if(list!=null&&list.size()>0){
					for(Map<String,Object> map :list){
						sb.append("<tr>");
						sb.append(setTd(map.get("user_id"),map.get("money"),map.get("register_time"),map.get("mobile_number")));
						sb.append("</tr>");
					}
				}
				sb.append("</table>");
				super.responseSendMsg(response, sb.toString(),"text/html");
			}
		} catch(CxdException e){
			super.cxdJsonException(json, e,value);
			sb.append(json.toString());
		}catch (Exception e) {
			super.jsonException(json, e, logger);
			sb.append(json.toString());
		}
		super.responseSendMsg(response, sb.toString(),"text/html");
	}	
	
	
	
	@RequestMapping(value = "getUserYeepayMessage", method = {RequestMethod.GET,RequestMethod.POST })
	public void getUserYeepayMessage(HttpServletRequest request,HttpServletResponse response,String value)throws IOException {
		super.requestLog("getUserYeepayMessage",value);
		StringBuffer sb = new StringBuffer();
		try {
			@SuppressWarnings("unchecked")
			Map<String, String> param = JSON.parseObject(value, HashMap.class);
			String loginId = param.get("loginId");
			String loginPassword = param.get("loginPassword");
			String userId = param.get("userId");
			ParamUtil.notNullParam("loginId:"+loginId,"loginPassword:"+loginPassword);
			
			if(!isShow(loginId,loginPassword)){
				sb.append(NO_SHOW_MSG);
			}else{
				String sql = "select id,mark_id , operator,request_data,request_time,response_data ,response_time,`status`,type,request_url from trusteeship_operation where request_data like '%<platformUserNo>"+userId+"</platformUserNo>%' ORDER BY request_time desc   LIMIT 0,100";
				List<Map<String,Object>> list = bt.queryListMap(sql);
				for(Map<String,Object> map :list){
					sb.append("id:"+map.get("id")+"\n");
					sb.append("mark_id:"+map.get("mark_id")+"\n");
					sb.append("operator:"+map.get("operator")+"\n");
					sb.append("状态:"+this.getYeePayStatus(map.get("status"))+"\n");
					sb.append("操作类型:"+this.getYeePayType(map.get("request_url"))+"\n");
					sb.append("访问地址:"+map.get("request_url")+"\n");
					sb.append("客户端类型:"+this.getClientType(map.get("request_url"))+"\n");
					sb.append("请求时间:"+map.get("request_time")+"\n\n");
					sb.append(this.getRequestData(map.get("request_data").toString().trim(),map.get("request_url"))+"\n\n");
					sb.append("-------------------------------------------------------------------------------------------------------------------------------------\n");
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		super.responseSendMsg(response, sb.toString());
	}
	
	private String getRequestData(String requestData,Object requestUrl){
		if(requestUrl==null || requestData.indexOf("sign=") < 0){
			return requestData;
		}
		Map<String, String> params = new HashMap<String, String>();
		int si = requestData.indexOf("sign=");
		int ri = requestData.indexOf("req=");
		if(si==0){
			params.put("req", StringEscapeUtils.escapeHtml(requestData.substring(ri+4).replace(" ", "").replace(",", "")));
			params.put("sign", requestData.substring(5,ri).replace(" ", "").replace(",", ""));
		}
		if(ri==0){
			params.put("req", StringEscapeUtils.escapeHtml(requestData.substring(4,si)));
			params.put("sign", requestData.substring(si+4));
		}
		return HtmlElementUtil.createAutoSubmitForm(params,requestUrl.toString().trim(),"UTF-8");
	}
	/* 获取发送易宝数据类型状态 */
	private String getYeePayStatus(Object status){
		if(status.equals("un_send")){
			return "等待发送";
		}else if(status.equals("sended")){
			return "已发送";
		}else if(status.equals("passed")){
			return "通过";
		}else if(status.equals("refused")){
			return "未通过";
		}else if(status.equals("no_response")){
			return "无响应";
		}else if(status.equals("cancel")){
			return "主动流标";
		}else if(status.equals("un_card_ing")){
			return "解绑审核中";
		}else{
			return "数据异常";
		}
	}
	/* 获取操作类型 */
	private String getYeePayType(Object url){
		if(url==null){
			return "操作类型为空";
		}
		if(url.toString().indexOf("toBindBankCard")>0){
			return "绑卡";
		}else if(url.toString().indexOf("toRecharge")>0){
			return "充值";
		}else if(url.toString().indexOf("toTransfer")>0){
			return "投资";
		}else if(url.toString().indexOf("toRegister")>0){
			return "实名认证";
		}else if(url.toString().indexOf("toWithdraw")>0){
			return "提现";
		}else if(url.toString().indexOf("toResetPassword")>0){
			return "充值密码";
		}else if(url.toString().indexOf("toResetMobile")>0){
			return "修改手机号";
		}else if(url.toString().indexOf("toUnbindBankCard")>0){
			return "解绑银行卡";
		}else if(url.toString().indexOf("toEnterpriseRegister")>0){
			return "企业实名认证";
		}else{
			return "为发现操作类型";
		}
	}
	/* 获取客户端类型*/
	private String getClientType(Object url){
		if(url==null){
			return "操作类型为空";
		}
		if("bhawireless".indexOf(url.toString())>0){
			return "移动端";
		}else{
			return "pc";
		}
	}
	
	/**新的获取最新公告
	 * @author wtj
	 * @date 2016年3月9日下午3:51:26
	 * @param request
	 * @param response
	 * @param value
	 * @throws IOException
	 */
	@RequestMapping(value = "appLog", method = {RequestMethod.GET,RequestMethod.POST })
	public void appLog(HttpServletRequest request,HttpServletResponse response,String value)throws IOException {
		JSONObject json = new JSONObject();
		try {
			json = otherService.appLog(json,value);
			if(json.get("status")==null){
				this.jsonMsg(json, 200, "成功");
			}
			json.put("result", new String[]{});
		} catch(CxdException e){
			super.cxdJsonException(json, e,value);
		}catch (Exception e) {
			super.jsonException(json, e, logger);
		}
		super.responseSendMsg(response,json);
	} 
}
